package com.br.negocio.web.backbean;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.br.negocio.controller.ControllerCargo;
import com.br.negocio.vo.CargoVO;
import com.br.negocio.web.formbean.CargoFormBean;

@Controller("cargoBackBean")
@Scope("session")
public class CargoBackBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Resource
	private ControllerCargo controllerCargo;
	
	private CargoFormBean cargoFormBean;

	public CargoBackBean() {
		super();
		this.cargoFormBean = new CargoFormBean();
	}

	public CargoFormBean getCargoFormBean() {
		return this.cargoFormBean;
	}

	public void setCargoFormBean(CargoFormBean cargoFormBean) {
		this.cargoFormBean = cargoFormBean;
	}
	
	// M�TODOS

	public void obterListaCargo() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.cargoFormBean.setListaCargo(this.controllerCargo.obterListaCargo());
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao obter a lista de cargos: " + e.getMessage()));
		}
	}
	
	public void manterCargo() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.controllerCargo.atualizarCargo(this.cargoFormBean.getCargoManter());
			this.cargoFormBean.setIsDisabled(true);
			context.addMessage(null, new FacesMessage("Sucesso: Inser��o realizada com sucesso!"));
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao manter cargo: " + e.getMessage()));
		}

	}
	
	public void excluirCargo() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			if (this.getCargoFormBean().getCargoManter().getCodigo() != 0) {
				this.controllerCargo.removerCargo(this.cargoFormBean.getCargoManter());
				if (!this.cargoFormBean.getListaCargo().isEmpty()) {
					this.cargoFormBean.setListaCargo(this.controllerCargo.obterListaCargo());
				}
				this.cargoFormBean.setCargoManter(new CargoVO());
				context.addMessage(null, new FacesMessage("Sucesso: Cargo exclu�do com sucesso!"));
			}
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao excluir o cargo: " + e.getMessage()));
		}
	}
	
	// LISTENERS
	
	public void listenerIrInserirCargo(ActionEvent evento) {
		this.cargoFormBean.setCargoManter(new CargoVO());
		this.cargoFormBean.setIsDisabled(false);
		this.cargoFormBean.setHead("Cadastrar Cargo");
	}
	
	public void listenerIrExcluirCargo(ActionEvent evento) {
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoCargoExcluir");
		int codigo = Integer.valueOf((Integer)comp.getValue());
		
		if (codigo != 0) {
			CargoVO cargoVO = new CargoVO();
			cargoVO.setCodigo(codigo);
			this.cargoFormBean.setCargoManter(this.controllerCargo.obterCargo(cargoVO));
		}
		this.cargoFormBean.setHead("Excluir Cargo");
		this.cargoFormBean.setIsDisabled(true);
	}
	
	public void listenerIrEditarCargo(ActionEvent evento) {
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoCargoEditar");
		int codigo = Integer.valueOf((Integer)comp.getValue());
		
		if (codigo != 0) {
			CargoVO cargoVO = new CargoVO();
			cargoVO.setCodigo(codigo);
			this.cargoFormBean.setCargoManter(this.controllerCargo.obterCargo(cargoVO));
		}
		
		this.cargoFormBean.setIsDisabled(false);
		this.cargoFormBean.setHead("Editar Cargo");
	}

}
